using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using PlayNexus.Classes;
using Yoshi.Classes;
using jQueryApi;

namespace ThemeChain
{
    [IgnoreNamespace]
    public class Theme : ThemeYoshi
    {
        private static readonly bool Initialized = Once();

        /// <summary>
        /// Dynamic overrides performed once upon loading
        /// </summary>
        /// <returns></returns>
        private static bool Once()
        {
            Helpers.StaticInheritClass(typeof (Theme).Name, typeof (ThemeYoshi).Name);
            Inform.Debug("Theme.Once() start");
            new Await()
                .LoadScript(Uri.App("sc/PlaySpace.js"))
                .LoadCss(Uri.App("cs/PlaySpace.css"))
                .AddDx(
                    delegate
                    {
                        Inform.Log("Theme.Once()");
                        Update();
                        jQuery.Document.Bind("F2Event", FunctionKeys.F2);
                        Master.MasterPage();
                    })
                .Commit();

            return !Initialized; // To make ReSharper happy that Initialized get's used
        }


        /// <summary>
        /// Reenterant dynamic overrides performed in Master.cs for every new page load
        /// </summary>
        /// <returns></returns>
        [PreserveName, PreserveCase]
#pragma warning disable 108,114
        public static void Update()
#pragma warning restore 108,114
        {
            PagesDataPath = Uri.Join(AppPath, PagesPartition);
            BgImgUrl = Uri.Versionize("url(" + Uri.Join(PagesDataPath, CurrentPageName, "background.jpg") + ")");
        }
    }
}